"use strict";
cc._RF.push(module, '7aca9l0679G3Ylrv84ZnwlL', 'peopleArcheryManage');
// scripts/engines/managers/peopleArcheryManage.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../gameEngine");
var CsvFileKind_1 = require("../libs/CsvFileKind");
var superManager_1 = require("./superManager");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var peopleArcheryManage = /** @class */ (function (_super) {
    __extends(peopleArcheryManage, _super);
    function peopleArcheryManage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.normalPeopleData = {
            id: "1",
            level: 1,
        };
        /**数据 */
        _this._data = {
            atkAddition: 0,
            revitAlize: 0,
            infinityLevel: 0,
            unlockRole: [_this.normalPeopleData],
            money: 0,
            adNum: {},
            curUseRoleId: "1",
            curInfinityLevel: 0,
        };
        /**初始数据 用于还原 */
        _this._initData = "";
        /**缓存的key */
        _this.save_data_key = "user-data-peopleArchery-save-key";
        /**配置表是否加载完 */
        _this.configLoadFlag = false;
        return _this;
    }
    peopleArcheryManage.prototype.loadManager = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                this.loadData();
                return [2 /*return*/];
            });
        });
    };
    peopleArcheryManage.prototype.getConfigComplete = function () {
        return this.configLoadFlag;
    };
    /**加载数据 */
    peopleArcheryManage.prototype.loadData = function () {
        this._initData = JSON.stringify(this._data);
        var dataStr = gameEngine_1.default.getServices().localStorageSrv.getStorage(this.save_data_key) || this._initData;
        var data = JSON.parse(dataStr);
        this.setData(data, false);
        var peoples = this.getData().unlockRole;
        for (var _i = 0, peoples_1 = peoples; _i < peoples_1.length; _i++) {
            var people = peoples_1[_i];
            for (var key in this.normalPeopleData) {
                if (people[key] == null) {
                    if (this.normalPeopleData[key] instanceof Array) {
                        people[key] = this.normalPeopleData[key].concat();
                    }
                    else if (this.normalPeopleData[key] instanceof Object) {
                        people[key] = JSON.parse(JSON.stringify(this.normalPeopleData[key]));
                    }
                    else {
                        people[key] = this.normalPeopleData;
                    }
                }
            }
        }
        this.writeData();
    };
    /**获取数据 */
    peopleArcheryManage.prototype.getData = function () {
        return this._data;
    };
    /**
     * 设置数据
     * @param data
     * @param isPlus
     */
    peopleArcheryManage.prototype.setData = function (data, isPlus) {
        if (isPlus === void 0) { isPlus = true; }
        for (var key in data) {
            var d = data[key];
            if (isPlus) {
                if (d instanceof Array) {
                    this._data[key] = this._data[key].concat(d);
                }
                else if (typeof d == "number") {
                    this._data[key] += d;
                }
                else {
                    this._data[key] = d;
                }
            }
            else {
                this._data[key] = d;
            }
        }
        this.writeData();
    };
    /**写入数据 */
    peopleArcheryManage.prototype.writeData = function () {
        gameEngine_1.default.getServices().localStorageSrv.setStorage(this.save_data_key, JSON.stringify(this._data));
    };
    /**重置manager */
    peopleArcheryManage.prototype.resetManager = function () {
        this._data = JSON.parse(this._initData);
        this.writeData();
    };
    peopleArcheryManage.prototype.loadConfig = function () {
        return __awaiter(this, void 0, void 0, function () {
            var roleIds, roles, _i, roleIds_1, id, file, fileName, talentIds, talents, _a, talentIds_1, id, file, fileName, roleComplete, roleCount, talentComplete, talentCount, fun;
            var _this = this;
            return __generator(this, function (_b) {
                roleIds = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole);
                roles = [];
                for (_i = 0, roleIds_1 = roleIds; _i < roleIds_1.length; _i++) {
                    id = roleIds_1[_i];
                    file = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole, id);
                    fileName = file["role_file"];
                    roles.push(fileName);
                }
                talentIds = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryTalent);
                talents = [];
                for (_a = 0, talentIds_1 = talentIds; _a < talentIds_1.length; _a++) {
                    id = talentIds_1[_a];
                    file = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryTalent, id);
                    fileName = file["talent_file"];
                    talents.push(fileName);
                }
                roleComplete = 0;
                roleCount = roleIds.length;
                talentComplete = 0;
                talentCount = talentIds.length;
                fun = function () {
                    var isTrue = roleComplete >= roleCount && talentComplete >= talentCount;
                    return isTrue;
                };
                return [2 /*return*/, new Promise(function (resolve) {
                        for (var _i = 0, roles_1 = roles; _i < roles_1.length; _i++) {
                            var role = roles_1[_i];
                            gameEngine_1.default.getManagers().csvDataManager.loadCsvFile("config/csv/peopleArchery/role/" + role, false, function () {
                                roleComplete++;
                                _this.configLoadFlag = fun();
                                if (_this.configLoadFlag) {
                                    _this.loadUnlockRole();
                                    resolve();
                                }
                            });
                        }
                        for (var _a = 0, talents_1 = talents; _a < talents_1.length; _a++) {
                            var talent = talents_1[_a];
                            gameEngine_1.default.getManagers().csvDataManager.loadCsvFile("config/csv/peopleArchery/talent/" + talent, false, function () {
                                talentComplete++;
                                _this.configLoadFlag = fun();
                                if (_this.configLoadFlag) {
                                    _this.loadUnlockRole();
                                    resolve();
                                }
                            });
                        }
                    })];
            });
        });
    };
    peopleArcheryManage.prototype.addOneRole = function (roleId) {
        var hasFlag = this.getRoleData(roleId) != null;
        if (hasFlag) {
            return;
        }
        var data = {};
        for (var key in this.normalPeopleData) {
            var d = this.normalPeopleData[key];
            if (d instanceof Array) {
                data[key] = d.concat();
            }
            else {
                data[key] = JSON.parse(JSON.stringify(d));
            }
        }
        data.id = roleId;
        this.setData({ unlockRole: [data] }, true);
    };
    peopleArcheryManage.prototype.getNormalPeopleData = function () {
        return this.normalPeopleData;
    };
    peopleArcheryManage.prototype.getRoleData = function (roleId) {
        var data = this.getData().unlockRole.find(function (people) {
            return people.id == roleId;
        });
        // if(data)
        return data;
        // let pData:peopleArcheryRole = <peopleArcheryRole>{};
        // for(let key in this.normalPeopleData){
        //     if(this.normalPeopleData[key] instanceof Array){
        //         pData[key] = this.normalPeopleData[key].concat();
        //     }else{
        //         pData[key] = this.normalPeopleData[key];
        //     }
        // }
        // pData["id"] = roleId;
        // return pData;
    };
    peopleArcheryManage.prototype.loadUnlockRole = function () {
        var ids = gameEngine_1.default.getManagers().csvDataManager.getCsvIds(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole);
        for (var _i = 0, ids_1 = ids; _i < ids_1.length; _i++) {
            var id = ids_1[_i];
            var pData = gameEngine_1.default.getManagers().csvDataManager.getCsvDataItem(CsvFileKind_1.CsvFileKind.CsvFile.peopleArcheryRole, id);
            var pUnlockType = pData.role_unlock_type;
            if (pUnlockType == "0") {
                this.addOneRole(id);
            }
        }
    };
    peopleArcheryManage.prototype.parseDataAsParam = function (data) {
        var param = Number(data === null || data === void 0 ? void 0 : data.option_param);
        if (isNaN(param)) {
            param = 0;
        }
        return param;
    };
    peopleArcheryManage.prototype.parseDataAsParamText = function (data) {
        var type = Number(data.param_type);
        var param = data.option_param;
        var str = "";
        if (type == 2 || type == 8 || type == 9 || type == 12 || type == 13 || type == 14 || type == 15 || type == 16 || type == 17 || type == 18 || type == 19 || type == 20 || type == 21 || type == 22) {
            var num = Number((param * 100).toFixed(2));
            str = num > 0 ? "+" + num + "%" : num + "%";
        }
        else {
            str = "" + (param >= 0 ? "+" + param : param);
        }
        return str;
    };
    peopleArcheryManage.prototype.parseDataAsOptionText = function (data) {
        var type = Number(data.param_type);
        var paramTextAry = [
            "生命",
            "攻击",
            "攻速",
            "射程",
            "子弹",
            "移速",
            "永久攻击",
            "回血",
            "射击伤害",
            "血量收益",
            "撞击吸血",
            "永久回血",
            "伤害减免",
            "暴击率",
            "暴击伤害",
            "血量上限",
            "回血增强",
            "BOSS降低血量",
            "五秒回复",
            "射击吸血",
            "关卡修为",
            "击败BOSS回血",
            "子弹伤害",
        ];
        var text = paramTextAry[type];
        return text;
    };
    peopleArcheryManage.prototype.getOptionText = function (data) {
        return this.parseDataAsOptionText(data) + this.parseDataAsParamText(data);
    };
    peopleArcheryManage = __decorate([
        ccclass
    ], peopleArcheryManage);
    return peopleArcheryManage;
}(superManager_1.default));
exports.default = peopleArcheryManage;

cc._RF.pop();